package com.yanq.liancar.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.NoScrollViewPager;
import com.yanq.liancar.fragment.CFindFragment;
import com.yanq.liancar.fragment.CGuanjiaFragment;
import com.yanq.liancar.fragment.CHomeFragment;
import com.yanq.liancar.fragment.CMineFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CMainActivity extends AppCompatActivity implements View.OnClickListener {


    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.chome)
    RadioButton chome;
    @BindView(R.id.cfind)
    RadioButton cfind;
    @BindView(R.id.cguanjia)
    RadioButton cguanjia;
    @BindView(R.id.cmine)
    RadioButton cmine;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    private List<Fragment> fragments = new ArrayList<>();
    public ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cmain);
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  //底部向上
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//顶部向下
        mImmersionBar = ImmersionBar.with(this);

        chome.setOnClickListener(this);
        cfind.setOnClickListener(this);
        cguanjia.setOnClickListener(this);
        cmine.setOnClickListener(this);

        viewPager.setScroll(false);

        viewPager.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.dp_5));
        viewPager.setOffscreenPageLimit(4);

        CHomeFragment homeFragment = new CHomeFragment();
        CGuanjiaFragment guanjiaFragment = new CGuanjiaFragment();
        CFindFragment findFragment = new CFindFragment();
        CMineFragment mineFragment = new CMineFragment();
        fragments.add(homeFragment);
        fragments.add(findFragment);
        fragments.add(guanjiaFragment);
        fragments.add(mineFragment);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setCurrent(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        setCurrent(0);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chome:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.cfind:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.cguanjia:
                viewPager.setCurrentItem(2, false);
                break;
            case R.id.cmine:
                viewPager.setCurrentItem(3, false);
                break;
        }

    }

    public void setCurrent(int id) {
        switch (id) {
            case 0:
                radioGroup.check(R.id.chome);
                //Utils.setStatusTextColor(false, this);
                break;
            case 1:
                radioGroup.check(R.id.cfind);
                //Utils.setStatusTextColor(false, this);
                break;
            case 2:
                radioGroup.check(R.id.cguanjia);
                //Utils.setStatusTextColor(false, this);
                break;
            case 3:
                radioGroup.check(R.id.cmine);
                //Utils.setStatusTextColor(false, this);
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏
    }
}